package com.example.test0.MainAlgorithm;

import com.example.test0.mapper.GoodsMapper;
import com.example.test0.mapper.UserMapper;
import com.example.test0.model.Goods;
import com.example.test0.model.GoodsAndGoods;
import com.example.test0.model.UserLike;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import org.apache.coyote.Request;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.*;

@Component
public class MainRecommend {

    private final List<Goods> goodslist;
    private final List<UserLike> item;
    private final List<UserLike> useritem;

    public MainRecommend() {
        this.goodslist = new ArrayList<>();
        this.item = new ArrayList<>();
        this.useritem = new ArrayList<>();
    }

    public MainRecommend(List<Goods> goodslist) {
        this.goodslist = goodslist;
        this.item = new ArrayList<>();
        this.useritem = new ArrayList<>();
    }

    public MainRecommend(List<Goods> goodslist, List<UserLike> item) {
        this.goodslist = goodslist;
        this.item = item;
        this.useritem = new ArrayList<>();
    }

    public MainRecommend(List<Goods> goodslist, List<UserLike> item, List<UserLike> useritem) {
        this.goodslist = goodslist;
        this.item = item;
        this.useritem = useritem;
    }

    public List<Goods> AdminRecommend() {
        List<Goods> ret = this.goodslist;


        System.out.println(ret);
        return ret;
    }

    public List<Goods> UserRecommend() {       //用户，推荐算法
        List<Goods> ret = this.goodslist;

        /*reset ret*/
        for (Goods goods : ret) {
            goods.setHeat(0);
        }

        /*计算相似*/
        List<GoodsAndGoods> goodsAndGoodsList = new ArrayList<>();
        for (Goods goods1 : goodslist) {
            for (Goods goods2 : goodslist) {
                String goods1id = goods1.getId();
                String goods2id = goods2.getId();
//                boolean flag = false;
//                if(Objects.equals(goods1id, goods2id))
//                    continue;
//                for(GoodsAndGoods temp : goodsAndGoodsList){
//                    if((Objects.equals(goods1id, temp.getGoodsid1()) && Objects.equals(goods2id, temp.getGoodsid2())
//                            || (Objects.equals(goods2id, temp.getGoodsid1()) && Objects.equals(goods1id, temp.getGoodsid2())))){
//                        flag = true;
//                    }
//                }
//                if(flag)
//                    continue;

                if (!Objects.equals(goods1id, goods2id)) {
                    /* cal goods1 goods2 */
                    GoodsAndGoods goodsAndGoods = new GoodsAndGoods(goods1id, goods2id, CalLikeBetGoods(goods1, goods2));
                    goodsAndGoodsList.add(goodsAndGoods);
                    /* find userlike user and goods1 */
//                    Goods objgoods = null;
//                    float like = 0;
//                    for(UserLike userlike : useritem){
//                        if(Objects.equals(userlike.getGoodsid(), goods1id)){
//                            like = goodsAndGoods.getLike()*userlike.getLike();
//                            break;
//                        }
//                    }
//
//                    for(Goods goods : ret){
//                        if(Objects.equals(goods.getId(), goods2id)){
//                            goods.setHeat(goods.getHeat()+like);
//                            like = 0;
//                            break;
//                        }
//                    }

                    //goodsAndGoodsList.add(goodsAndGoods);
                }
            }
        }

        for (Goods goods1 : ret) {
            for (Goods goods2 : goodslist) {
                if (!Objects.equals(goods1.getId(), goods2.getId())) {
                    for (GoodsAndGoods goodsAndGoods : goodsAndGoodsList) {
                        if (Objects.equals(goods1.getId(), goodsAndGoods.getGoodsid1())
                                && Objects.equals(goods2.getId(), goodsAndGoods.getGoodsid2())) {
                            for (UserLike userLike : useritem) {
                                if (Objects.equals(userLike.getGoodsid(), goods1.getId())) {
                                    float like = userLike.getLike() * goodsAndGoods.getLike();
                                    goods2.setHeat(goods1.getHeat() + like);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        /* 推荐排序 */
        ret.sort(new Comparator<Goods>() {
            @Override
            public int compare(Goods o1, Goods o2) {
                return -(int) ((o1.getHeat() - o2.getHeat()) * 10000000);
            }
        });
        ret = ret.subList(0, 10);

        System.out.println(ret);
        return ret;
    }

    public List<Goods> VisitorRecommend() {          //游客，最火推荐
        List<Goods> ret = this.goodslist;
        if (ret != null) {
            ret.sort(new Comparator<Goods>() {
                @Override
                public int compare(Goods o1, Goods o2) {
                    return -(int) ((o1.getHeat() - o2.getHeat()) * 10000000);
                }
            });
            ret = ret.subList(0, 10);
        }

        System.out.println(ret);
        return ret;
    }

    public float CalLikeBetGoods(Goods tgoods1, Goods tgoods2) {
        float ret = 0;
        float ret1 = 0;
        float ret2 = 0;

        List<UserLike> itemlist1 = new ArrayList<>();
        List<UserLike> itemlist2 = new ArrayList<>();
        for (UserLike temp : item) {
            if (Objects.equals(temp.getGoodsid(), tgoods1.getId())) {
                itemlist1.add(temp);
                ret1 += temp.getLike() * temp.getLike();
            } else if (Objects.equals(temp.getGoodsid(), tgoods2.getId())) {
                itemlist2.add(temp);
                ret2 += temp.getLike() * temp.getLike();
            }
        }

        for (UserLike item1 : itemlist1) {
            for (UserLike item2 : itemlist2) {
                if (Objects.equals(item1.getUserid(), item2.getUserid())) {
                    ret += item1.getLike() * item2.getLike();
                }
            }
        }

        if (Math.sqrt((double) ret1) * Math.sqrt((double) ret2) != 0) {
            ret = (float) (ret / (Math.sqrt((double) ret1) * Math.sqrt((double) ret2)));
        }
        return ret;
    }


}
